/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package application.Arguments;

import org.apache.log4j.*;

import library.Constants.*;
import library.Configuration.*;

/**
 * This class is designed to process one argument of an application
 * Any argument is supposed to be formed like : <Commande><Keword>=<Value>
 * <Commande> : -X
 * <Keyword>=<Value> : is called the assignment
 * 
 * @author JNC
 */
public class ApplicationArgument {
    /**
     * Index of the char of a parameter that is the 1st char of the command
     */
    private static final int _START_COMMAND = 0;
    /**
     * Index of the char of a parameter that is the last char of the command
     * after the command
     */
    private static final int _START_ARGUMENT = 2;
    /**
     * This keyword indicates to process an assignment
     */
    private static final CharSequence _ASSIGNMENT_KEYWORD = "-X";
    /**
     * A copy of he argument
     */
    private String _argument;
    /**
     * The log4j log
     */
    private static final Logger _logger = Logger.getLogger(ApplicationArgument.class);
    
    /**
     * Constructor
     */
    public ApplicationArgument(String myArgument) {
        _argument = myArgument;
    }
    
    /**
     * Process an argument
     * @param myArgument
     */
    public void process() {
        _logger.debug("Processing argument " + _argument);

        // find the command among the first 2 letters
        processCommand(_argument.subSequence(_START_COMMAND,_START_ARGUMENT),
                       _argument.substring(_START_ARGUMENT));
    }

    /**
     * Process a command, and an assignment
     * @param myCommand
     * @param myAssignment
     * @return void
     */
    private void processCommand(CharSequence myCommand, String myAssignment) {
        
        if (myCommand.equals(_ASSIGNMENT_KEYWORD)) {
            _logger.debug("Found command : " + _ASSIGNMENT_KEYWORD);
            processAssignment(myAssignment);
        } else {
            _logger.fatal("Unknown command : " + myCommand + " with argument " + myAssignment);
        }
    }
    
    /**
     * Process an assignment
     * @param myAssignment
     */
    private void processAssignment(String myAssignment) {
        String[] lWords;
        
        // sets the delimiter in the assignment
        // ------------------------------------
        lWords = myAssignment.split("=");
            
        // checks that the assignment has 2 parts only
        // -------------------------------------------
        if (lWords.length != 2) {
            _logger.fatal("Incorrect assignement : " + myAssignment + " (syntax should be <var>=<value>)");

        // read the filename
        // -----------------
        } else if (lWords[0].equals(Constants._CONFIGFILENAME_KEYWORD)) {
            _logger.debug("Found keyword " + lWords[0]);
            _logger.debug("Found value " + lWords[1]);
            Configuration.setConfigFilename(lWords[1]); 
        
        // the keyword is unknown
        // ----------------------
        } else {
            _logger.fatal("Unknown assignment : " + myAssignment);
        }
    }
}
